#! /bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

echo "run"

if [ $(id -u) != "0" ]; then
    printf "Error: You must be root to run this script!\n"
    exit 1
fi

if [ "$1" = "start" ]; then
    psid='ps aux|grep "uwsgi"|grep -v "grep"|wc -l'
    if [ $psid -gt 2 ]; then
        echo "uwsgi is running!"
        exit 0
    else
        /usr/local/bin/uwsgi -x /home/dong/blog/mysite/django.xml
        /usr/sbin/nginx -s reload
    fi
    echo "Start uwsgi server [ok]"
elif [ "$1" = "stop" ]; then
    killall -9 uwsgi
    echo "Stop uwsgi server [ok]"
elif [ "$1" = "restart" ]; then
    killall -9 uwsgi
    /usr/local/bin/uwsgi -x /home/dong/blog/mysite/django.xml
    /usr/sbin/nginx -s reload
    echo "Restart uwsgi server [ok]"
else
    echo "User: uwsgiserver [start|stop|restart]"
fi
